<template>
  <div class="app-container">

        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
         current-node-key="1"
            accordion="true"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
  </div>
</template>

<script>
import {  deptTreeSelect } from "@/api/system/user";
import {
schoolTree
} from "@/api/information/index";

export default {
  name: "User",
props: {
    type: {
      type: Number,
      default: () => 1
    }
  },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined,
            // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getDeptTree();
  },
  methods: {

    /** 查询部门下拉树结构 */
    getDeptTree() {
      console.log("获取学校树",this.type)
      if (this.type=='2') {
        schoolTree().then(response => {
        this.deptOptions = response.data;
      });
      } else {
              deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
      }

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
        console.log("点击了学校",data)
this.$emit('accessSchool',data)
    },

  }
};
</script>